import request from '@/utils/request'
var prefix = '/pms'
// 通用请求接口
export function baseRequest(url, method, params = {}, data = {}, headers = {}) {
  return request({
    url,
    method,
    params,
    data,
    headers
  })
}
// 查询配置
export function getConfigInfo(query) {
  return request({
    url: prefix + '/pageConfig/getConfigData',
    method: 'get',
    params: query
  })
}
// 查询列表
export function getDataList(route, query) {
  return request({
    url: prefix + route,
    method: 'get',
    params: query
  })
}
// 从ES中查询数据
export function getEsData(id, name, operation = '') {
  if (operation) {
    return request({
      url: '/history/oper_history/_search',
      method: 'post',
      data: {
        query: {
          bool: {
            must: [
              { match: { docId: id } },
              { match: { module: name } },
              { match: { operation: operation } }
            ]
          }
        }
      }
    })
  } else {
    return request({
      url: '/history/oper_history/_search',
      method: 'post',
      data: {
        query: {
          bool: {
            must: [{ match: { docId: id } }, { match: { module: name } }]
          }
        }
      }
    })
  }
}

// 查询详细
export function getData(route, id) {
  if (id) {
    return request({
      url: prefix + route + '/' + id,
      method: 'get'
    })
  } else {
    return request({
      url: prefix + route,
      method: 'get'
    })
  }
}

// 新增
export function addData(route, data) {
  return request({
    url: prefix + route,
    method: 'post',
    data: data
  })
}

// 修改
export function updateData(route, data) {
  return request({
    url: prefix + route,
    method: 'put',
    data: data
  })
}

// 删除
export function delData(route, id) {
  return request({
    url: prefix + route + '/' + id,
    method: 'delete'
  })
}

// 导出
export function exportData(route, query) {
  return request({
    url: prefix + route + '/export',
    method: 'get',
    params: query
  })
}

// 树结构
export function getTreeData(route) {
  return request({
    url: prefix + route + '/treeData',
    method: 'get'
  })
}

// 获取文件夹数据
export function getFolderTree(route) {
  return request({
    url: prefix + route,
    method: 'get'
  })
}

// 获取新增表单数据
export function getAddForm(query) {
  return request({
    url: prefix + '/pageConfig/getAddForm',
    method: 'get',
    params: query
  })
}
// 获取表格列
export function getTableCol(query) {
  return request({
    url: prefix + '/pageConfig/getTableCol',
    method: 'get',
    params: query
  })
}

// 获取选项信息
export function getOptionsInfo() {
  return request({
    url: '/pms/pageConfig/getOptionsInfo',
    method: 'get'
  })
}

//获取TreeCommand信息
export function getTreeCommand(query) {
  return request({
    url: '/pms/pageConfig/getTreeCommand',
    method: 'get',
    params: query
  })
}

// 生命周期
// 提升
export function promote(params) {
  return request({
    url: '/pms/lifecycle/promote',
    method: 'get',
    params: params
  })
}
// 降级
export function demotion(params) {
  return request({
    url: '/pms/lifecycle/demotion',
    method: 'get',
    params: params
  })
}

// 升级降级
export function upgrade(route, id) {
  return request({
    url: prefix + route + '/' + id,
    method: 'get'
  })
}
// 文件删除
export function filedel(data) {
  return request({
    url: 'pms/doc/deleteFile',
    method: 'put',
    data
  })
}

// 权限验证
export function permissions(operateType, state) {
  return request({
    url:
      '/system/authority/permissions?operateType=' +
      operateType +
      '&state=' +
      state,
    method: 'get'
  })
}

// 权限验证
export function permissionsNoState(operateType, docId) {
  return request({
    url:
      '/system/authority/permissionsNoState?operateType=' +
      operateType +
      '&docId=' +
      docId,
    method: 'get'
  })
}

// 记录预览历史记录
export function previewRecord(docId, fileId) {
  return request({
    url: '/pms/log/download/previewRecord?docId=' + docId + '&fileId=' + fileId,
    method: 'get'
  })
}
// 表格列状态更改
export function changeStatus(url, obj) {
  return request({
    url: url,
    method: 'put',
    data: obj
  })
}
